package com.ruoyi.utils;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.constant.CsbsEnum;
import com.ruoyi.system.mapper.SysRoleMapper;

import java.util.Arrays;
import java.util.List;

/**
 * 任务工具
 */
public class RwUtil {

    /**
     * 矿业权处
     */
    private static List<String> KYQ_MARKS = Arrays.asList("KYQC_SHENG", "KYQC_SHI", "KYQC_XIAN");

    /**
     * 矿保处
     */
    private static List<String> KBC_MARKS = Arrays.asList("KBC_SHENG", "KBC_SHI", "KBC_XIAN");

    /**
     * 修复处
     */
    private static List<String> XFC_MARKS = Arrays.asList("XFC_SHENG", "XFC_SHI", "XFC_XIAN");

    /**
     * 执法局
     */
    private static List<String> ZFJ_MARKS = Arrays.asList("ZFJ_SHENG", "ZFJ_SHI", "ZFJ_XIAN");

    private static final String KYQ_MARK = "KYQ";
    private static final String KBC_MARK = "KBC";
    private static final String XFC_MARK = "XFC";
    private static final String ZFJ_MARK = "ZFJ";
    private static final String SYS_MANAGER = "system-manager";
    private static final String PROVINCE_MANAGER = "province-manager";
    private static final String CITY_MANAGER = "city-manager";
    private static final String COUNTY_MANAGER = "county-manager";

//    /**
//     * 根据处室标识获取省级用户角色 旧方法
//     *
//     * @param csbs
//     * @return
//     */
//    public static Long getProvinceRoleIdByCsbs(String csbs) {
//        SysRoleMapper roleMapper = SpringUtils.getBean(SysRoleMapper.class);
//        if (KYQ_MARK.equals(csbs)) {
//            SysRole role = roleMapper.selectRoleIdByRoleKey("130000_KYQC_SHENG");
//            return role.getRoleId();
//        } else if (KBC_MARK.equals(csbs)) {
//            SysRole role = roleMapper.selectRoleIdByRoleKey("130000_KBC_SHENG");
//            return role.getRoleId();
//        } else if (XFC_MARK.equals(csbs)) {
//            SysRole role = roleMapper.selectRoleIdByRoleKey("130000_XFC_SHENG");
//            return role.getRoleId();
//        } else if (ZFJ_MARK.equals(csbs)) {
//            SysRole role = roleMapper.selectRoleIdByRoleKey("130000_ZFJ_SHENG");
//            return role.getRoleId();
//        }
//        return null;
//    }

    /**
     * 根据处室标识获取省级用户角色 修改之后的方法
     *
     * @param csbs
     * @return
     */
    public static String getProvinceRoleIdByCsbs(String csbs) {
        SysRoleMapper roleMapper = SpringUtils.getBean(SysRoleMapper.class);
        if (KYQ_MARK.equals(csbs)) {
            return "130000_KYQC_SHENG";
        } else if (KBC_MARK.equals(csbs)) {
            return "130000_KBC_SHENG";
        } else if (XFC_MARK.equals(csbs)) {
            return "130000_XFC_SHENG";
        } else if (ZFJ_MARK.equals(csbs)) {
            return "130000_ZFJ_SHENG";
        }
        return null;
    }

//    /**
//     * 旧方法
//     * @param loginUser
//     * @return
//     */
//    public static Long getRoleIdByLoginUser(LoginUser loginUser) {
//        // 当前进入的处室标识
//        String csbs = loginUser.getCsbs();
//        String adcode = loginUser.getAdcode();
//        Integer regionLevel = loginUser.getRegionLevel();
//        String roleKey = adcode + "_";
//        if (KYQ_MARK.equals(csbs)) {
//            roleKey = roleKey + KYQ_MARKS.get(regionLevel - 1);
//        } else if (KBC_MARK.equals(csbs)) {
//            roleKey = roleKey + KBC_MARKS.get(regionLevel - 1);
//        } else if (XFC_MARK.equals(csbs)) {
//            roleKey = roleKey + XFC_MARKS.get(regionLevel - 1);
//        } else if (ZFJ_MARK.equals(csbs)) {
//            roleKey = roleKey + ZFJ_MARKS.get(regionLevel - 1);
//        }
//        SysRoleMapper roleMapper = SpringUtils.getBean(SysRoleMapper.class);
//        SysRole role = roleMapper.selectRoleIdByRoleKey(roleKey);
//        return role == null ? null : role.getRoleId();
//    }

    /**
     * 修改之后的方法
     * @param loginUser
     * @return
     */
    public static String getRoleIdByLoginUser(LoginUser loginUser) {
        // 当前进入的处室标识
        String csbs = loginUser.getCsbs();
        String adcode = loginUser.getAdcode();
        Integer regionLevel = loginUser.getRegionLevel();
        String roleKey = adcode + "_";
        if (KYQ_MARK.equals(csbs)) {
            roleKey = roleKey + KYQ_MARKS.get(regionLevel - 1);
        } else if (KBC_MARK.equals(csbs)) {
            roleKey = roleKey + KBC_MARKS.get(regionLevel - 1);
        } else if (XFC_MARK.equals(csbs)) {
            roleKey = roleKey + XFC_MARKS.get(regionLevel - 1);
        } else if (ZFJ_MARK.equals(csbs)) {
            roleKey = roleKey + ZFJ_MARKS.get(regionLevel - 1);
        }
        return roleKey;
    }

//    /**
//     * 通过角色id获取处室标识
//     *
//     * @param roleId
//     * @return
//     */
//    public static String getCsbsByRoleId(Long roleId) {
//        SysRoleMapper roleMapper = SpringUtils.getBean(SysRoleMapper.class);
//        SysRole sysRole = roleMapper.selectRoleById(roleId);
//        String roleKey = sysRole.getRoleKey();
//        if ("admin".equals(roleKey)) {
//            return SYS_MANAGER;
//        }
//        if ("130000_ADMIN_SHENG".equals(roleKey)) {
//            return PROVINCE_MANAGER;
//        }
//        if ("ADMIN_SHI".contains(roleKey.substring(7))){
//            return CITY_MANAGER;
//        }
//        if ("ADMIN_XIAN".contains(roleKey.substring(7))){
//            return COUNTY_MANAGER;
//        }
//        // roleKey 符合 130100_KYQC_SHI 格式要求
//        String _roleKey = roleKey.substring(7);
//        if (KYQ_MARKS.contains(_roleKey)) {
//            return KYQ_MARK;
//        } else if (KBC_MARKS.contains(_roleKey)) {
//            return KBC_MARK;
//        } else if (XFC_MARKS.contains(_roleKey)) {
//            return XFC_MARK;
//        } else if (ZFJ_MARKS.contains(_roleKey)) {
//            return ZFJ_MARK;
//        }
//        return roleKey;
//    }

    /**
     * 通过角色id获取处室标识
     *
     * @param roleKey
     * @return
     */
    public static String getCsbsByRoleId(String roleKey) {
        if ("admin".equals(roleKey)) {
            return SYS_MANAGER;
        }
        if ("130000_ADMIN_SHENG".equals(roleKey)) {
            return PROVINCE_MANAGER;
        }
        if ("ADMIN_SHI".contains(roleKey.substring(7))){
            return CITY_MANAGER;
        }
        if ("ADMIN_XIAN".contains(roleKey.substring(7))){
            return COUNTY_MANAGER;
        }
        // roleKey 符合 130100_KYQC_SHI 格式要求
        String _roleKey = roleKey.substring(7);
        if (KYQ_MARKS.contains(_roleKey)) {
            return KYQ_MARK;
        } else if (KBC_MARKS.contains(_roleKey)) {
            return KBC_MARK;
        } else if (XFC_MARKS.contains(_roleKey)) {
            return XFC_MARK;
        } else if (ZFJ_MARKS.contains(_roleKey)) {
            return ZFJ_MARK;
        }
        return roleKey;
    }
//    /**
//     * 通过角色id获取处室标识名称
//     *
//     * @param roleId
//     * @return
//     */
//    public static String getCsbsNameByRoleId(Long roleId) {
//        String csbs = getCsbsByRoleId(roleId);
//        String desc = CsbsEnum.getDescByCode(csbs);
//        if (desc == null) {
//            throw new ServiceException("无此" + csbs + "标识对应的处室");
//        }
//        return desc;
//    }

    /**
     * 通过角色id获取处室标识名称
     *
     * @param roleKey
     * @return
     */
    public static String getCsbsNameByRoleId(String roleKey) {
        String csbs = getCsbsByRoleId(roleKey);
        String desc = CsbsEnum.getDescByCode(csbs);
        if (desc == null) {
            throw new ServiceException("无此" + csbs + "标识对应的处室");
        }
        return desc;
    }

    public static Integer decideRegionLevel(String adcd) {
        String endFourChar = adcd.substring(2);
        String endTwoChar = adcd.substring(4);
        if ("0000".equals(endFourChar)) {
            return 1;
        } else if ("00".equals(endTwoChar)) {
            return 2;
        } else {
            return 3;
        }
    }

}
